<?php


class Movie extends Controller
{
	/**
	 * 解析函数
	 *
	 */
	function __construct()
	{
		parent::__construct();
		$this->loadModel('Category_model');
		$this->loadModel('Movie_model');
		$this->loadModel('Language_model');
		$this->loadModel('Area_model');
		$this->loadModel('Server_model');
		$this->loadModel('Type_model');
		$this->loadClass('html');

		include_once(INCLUDE_DIR.'pscws/pscws2.class.php');
		$this->pscws = new PSCWS2(INCLUDE_DIR.'pscws/dict/dict.xdb');
		$this->pscws->set_ignore_mark(true);
		$this->pscws->set_autodis(true);
	}

	function showDate()
	{

		if(!empty($this->in_var['CategoryID']))
		{
			$this->Movie_model->where("m.CategoryID",$this->in_var['CategoryID']);
			$param .= '&CategoryID='.$this->in_var['CategoryID'];
		}

		if(!empty($this->in_var['keywords']))
		{
			$keywords = htmlspecialchars(trim(urldecode($this->in_var['keywords'])));
			$this->Movie_model->where("m.Name",$keywords,'like');
			$param .= '&keywords='.urlencode($this->in_var['keywords']);
		}

		if($this->in_var['State'] == -1)
		{
			$this->Movie_model->where("m.State",-1);
			$param .= '&State=-1';
		}
		else
		{
			$this->Movie_model->where("m.State",1);
			$param .= '&State=1';
		}

		$page = !empty($this->in_var['page']) ? $this->in_var['page'] : 1;
		//$num  = !empty($this->in_var['num'])  ? $this->in_var['num']  : $this->sys_info['PageNum'];
		$num  = !empty($this->in_var['num'])  ? $this->in_var['num']  : 10;

		$sort = $this->in_var['sort'] ? $this->in_var['sort'] : '-m.Id';
		
		$order = $this->Movie_model->getOrder($sort);

		$result_list     		= $this->Movie_model->getMovieList('*',$order,$page,$num);
		$result_page_info 		= $this->Movie_model->getPageInfo('?c=movie&d=showDate&page={Page}&sort='
		. $this->in_var['sort']
		. $param);

		
		$this->tpl->assign('ResultPageInfo',$result_page_info);
		$this->tpl->assign("ResultList", $result_list);
		$this->tpl->display('movie_list.html');    //指定模板
	}
	
	/**
	 * 添加影片
	 *
	 */
	function add()
	{
		if($this->in_var['request_method'] == 'post')
		{

		}

		$category_for_select = $this->Category_model->getCategoryForSelect(0,$this->in_var['CategoryID']);
		
		$language 	= $this->Language_model->getLanguageyList();
		$area 		= $this->Area_model->getAreaList();
		$server 	= $this->Server_model->getServerList();
		$type 		= $this->Type_model->getTypeList();
		
		$this->tpl->assign('LanguageList',$language);
		$this->tpl->assign('AreaList',$area);
		$this->tpl->assign('ServerList',$server);
		$this->tpl->assign('TypeList',$type);
		$this->tpl->assign('MovieInfo',$movie_info);
		$this->tpl->assign('CategoryForSelect',$category_for_select);
		$this->tpl->display('movie_add.html');    //指定模板
	}

	/**
	 * 文章编辑
	 *
	 */
	function edit()
	{
		if($this->in_var['request_method'] == 'post')
		{

			if(empty($this->in_var['Name']))
			{
				show_message('影片名没有填写哦',$_SERVER['HTTP_REFERER'],3);
			}

			//影片名分词
			$keywords = $this->pscws->segment($this->in_var['Name']);
			foreach ($keywords as $word){
				$fulltxt_name.= b64encode($word)." ";
			}

			$category_list = $this->category_list[$this->in_var['CategoryID']]['CategoryName'] .',' .$this->in_var['SubCategoryName'];
			$category_list = str_to_category($category_list);

			//分类分词
			foreach ($category_list as $t){
				$keywords = $this->pscws->segment($t['CategoryName']);
				foreach ($keywords as $word){
					$fulltxt_category.= b64encode($word)." ";
				}
			}

			//演员分词
			$keywords = $this->pscws->segment($this->in_var['Actor']);
			foreach ($keywords as $actor){
				$fulltxt_actor.= b64encode($actor)." ";
			}

			//专辑分词
			$keywords = $this->pscws->segment($this->in_var['Special']);
			foreach ($keywords as $special){
				$fulltxt_special.= b64encode($special)." ";
			}
			

			$fulltxt = $fulltxt_name . $fulltxt_category . $fulltxt_actor . $fulltxt_special;

			$article_content = $this->in_var['Content'];
			//删除文章内的链接
			if($this->in_var['Dellink'] == '1'){

				$article_content = ereg_replace("<a([^>]*)>","",$article_content);
				$article_content = ereg_replace("</a>","",$article_content);

			}

			//返回二维数组
			$actors_list = str_to_actors($this->in_var['Actor']);

			//进入演员表
			if(is_array($actors_list))
			{
				foreach ($actors_list as $key=>$value)
				{
					$actor_info = $this->getDataInfo('actor',' where Name = "' .trim($value['Name']) .'"');
					//新增
					if(empty($actor_info))
					{
						if(!empty($value['Name']))
						{
							$this->cleanData();
							$this->addData('Num',1);
							$this->addData('Name',trim($value['Name']));
							$this->dataInsert('actor');
						}
					}
					else//统计数量
					{
						$this->cleanData();
						$this->addData('Num',$actor_info['Num']+1);
						$this->dataUpdate('actor',' where Name = "' .trim($value['Name']) .'"');
					}
				}
			}

			$this->cleanData();
			$this->addData('CategoryID',$this->in_var['CategoryID']);
			$this->addData('Type',$this->in_var['Type']);
			$this->addData('SubCategoryID',$this->in_var['SubCategoryID']);
			$this->addData('SubCategoryName',$this->in_var['SubCategoryName']);
			$this->addData('SpecialID',$this->in_var['SpecialID']);
			$this->addData('Special',$this->in_var['Special']);
		
			if($this->in_var['UpdateDate'] == 1)
			{
				$this->addData('UpdateDate',time());
			}
			
			$this->addData('State',$this->in_var['State']);
			//$this->addData('UserID',$_SESSION['yanghuan_session']['UserID']);
			$this->addData('Hits',$this->in_var['Hits']);
			$this->addData('Top',$this->in_var['Top']);
			$this->addData('Mark',$this->in_var['Mark']);

			$this->addData('Name',$this->in_var['Name']);
			$this->addData('KeyWords',$this->in_var['KeyWords']);
			$this->addData('Language',$this->in_var['Language']);
			$this->addData('Area',$this->in_var['Area']);
			$this->addData('Actor',$this->in_var['Actor']);
			$this->addData('Year',$this->in_var['Year']);
			$this->addData('DireCtor',$this->in_var['DireCtor']);
			$this->addData('PlayDate',$this->in_var['PlayDate']);
			$this->addData('Content',$article_content);
			$this->addData('IsHD',$this->in_var['IsHD']);
			$this->addData('IsNew',$this->in_var['IsNew']);
			$this->addData('IsIndex',$this->in_var['IsIndex']);
			$this->addData('IsRed',$this->in_var['IsRed']);
			$this->addData('IsComment',$this->in_var['IsComment']);
			//全文检索
			$this->addData('FullTxt',$fulltxt);
			$this->addData('FullTxtName',$fulltxt_name);
			$this->addData('FullTxtCategory',$fulltxt_category);
			$this->addData('FullTxtActor',$fulltxt_actor);

			if($this->dataUpdate('movie','where Id = ' . $this->in_var['Id']))
			{
				$movie_info = $this->getDataInfo('movie','where Id = ' .$this->in_var['Id']);
				//获取ID
				$movie_id = $this->in_var['Id'];
				//处理相关分类
				$category_list = explode(',',$this->in_var['SubCategoryID']);
				//添加主分类
				$category_list[] = $this->in_var['CategoryID'];
				//去除重复ID
				$category_list = array_unique($category_list);

				//判断是否为数组
				if(is_array($category_list))
				{
					//初始化分类
					$this->dataDel('movie_index','where MovieID = ' . $movie_id);
					foreach ($category_list as $value)
					{
						//判断数据是否为空
						if($value)
						{
							$moveie_info = $this->getDataInfo('movie_index','where MovieID = '.$movie_id .' and CategoryID= '.$value);
							//判断是否已经存在
							if(empty($moveie_info))
							{
								$this->cleanData();
								$this->addData('MovieID',$movie_id);
								$this->addData('CategoryID',$value);
								$this->dataInsert('movie_index');
							}

						}

					}
				}

				//处理专题
				$specia_list = explode(',',$this->in_var['SpecialID']);
				//判断是否为数组
				if(is_array($specia_list))
				{
					//初始化分类
					$this->dataDel('special_index','where MovieID = ' . $movie_id);
					foreach ($specia_list as $value)
					{
						//判断数据是否为空
						if($value)
						{
							$moveie_info = $this->getDataInfo('special_index','where MovieID = '.$movie_id .' and SpecialID= '.$value);
							//判断是否已经存在
							if(empty($moveie_info))
							{
								$this->cleanData();
								$this->addData('MovieID',$movie_id);
								$this->addData('SpecialID',$value);
								$this->dataInsert('special_index');
							}

						}

					}
				}

				//处理新的电影列表
				if($this->in_var['count'])
				{
					for ($i=1;$i <= $this->in_var['count'];$i++){

						if($this->in_var['Name_'.$i])
						{
							$this->cleanData();
							$this->addData('MovieID',$movie_id);
							$this->addData('Server',$this->in_var['Server_'.$i]);
							$this->addData('Name',$this->in_var['Name_'.$i]);
							$this->addData('URL',$this->in_var['URL_'.$i]);
							$this->addData('Top',$this->in_var['Top_'.$i]);
							$this->addData('PublishDate',time());
							$this->dataInsert('url');
							$this->cleanData();
						}
					}
				}
				
				//修改原来的数据
				if($this->in_var['editcount'])
				{
					$num  = $this->in_var['editcount'] + $this->in_var['count'];
					for ($i=1;$i <= $num;$i++){

						if($this->in_var['edit_name_'.$i])
						{
							$this->cleanData();

							$this->addData('MovieID',$movie_id);
							$this->addData('Server',$this->in_var['edit_server_'.$i]);
							$this->addData('Name',$this->in_var['edit_name_'.$i]);
							$this->addData('URL',$this->in_var['edit_url_'.$i]);
							$this->addData('Top',$this->in_var['edit_orderby_'.$i]);
							$this->addData('PublishDate',time());
							$this->dataUpdate('url','where Id='.$this->in_var['edit_id_'.$i]);
							$this->cleanData();
						}
					}
				}
				
				//处理上传
				$dir =  $this->sys_info['UploadPath'] .'/'. Date('m/',time()) . $movie_id . '/';
				$file_name = CsubStr(strtoupper(md5(serialize($_FILES['Photo']))),0,10,'');

				if(!empty($_FILES['Photo']['name']))
				{
					$this->html->createdir($dir);
					$this->upload->setUploadDir($dir);
					$this->upload->setUploadFileName($file_name);
					$this->upload->uploadFile('Photo');
					unlink('../'.$movie_info['Path'] . $movie_info['Photo']);
					$this->cleanData();
					$this->addData('Path',str_replace('../','',$dir));
					$this->addData('Photo',$this->upload->upload_file);
					$this->dataUpdate('movie','where Id = ' . $movie_id);
				}
				elseif(!empty($this->in_var['Photo']))
				{
					//定义全部变量，传入localImg供生成目录
					$photo = localImg($this->in_var['Photo'],$dir);
					unlink('../'.$movie_info['Path'] . $movie_info['Photo']);
					$this->cleanData();
					$this->addData('Path',str_replace('../','',$photo['Path']));
					$this->addData('Photo',$photo['Photo']);
					$this->dataUpdate('movie','where Id = '.$movie_id);
				}

				$Msg = '数据更新成功..';
			}
			else
			{
				$Msg = '失败拉。请注意检查..';
			}
		}

		$language = $this->getDataList('language','order by Top desc , Id desc');
		$area = $this->getDataList('area','order by Top desc , Id desc');
		$server_list = $this->getDataList('server','order by Top desc , Sid desc');

		$num = $this->dataNum('url', ' where MovieID = '.$this->in_var['id']);

		$movie_info = $this->getDataInfo('movie','where Id = ' . $this->in_var['id']);

		$this->tpl->assign('num',$num + 1);
		$this->tpl->assign('Msg',$Msg);
		$this->tpl->assign('Language',$language);
		$this->tpl->assign('Area',$area);
		$this->tpl->assign('ServerList',$server_list);
		$this->tpl->assign('MovieInfo',$movie_info);
		$this->tpl->assign('CategoryForSelect',$this->getCategoryForSelect(0,$movie_info['CategoryID']));
		$this->template = 'movie_edit.html';    //指定模板

	}

	/**
	 * 删除编辑
	 *
	 */
	//删除信息
	function delete()
	{

		$id = explode(',',$this->in_var['id']);

		if(empty($this->in_var['id']))
		{
			show_message('您想干什么？',$_SERVER['HTTP_REFERER']);exit;
		}
		elseif(!empty($id['0']))
		{

			foreach ($id as $value)
			{
				$movie_info = $this->getDataInfo('movie','where Id = ' . $value);
				$dir = $this->sys_info['UploadPath'] .'/'. Date('m/',$movie_info['PublishDate']) . $movie_info['Id'] . '/';
				$this->html->deletedir($dir);
				//@unlink('../' . $article_info['Photo']);
				$this->dataDel('movie','Where Id ='.$value);
				$this->dataDel('movie_index','Where MovieID ='.$value);
				$this->dataDel('special_index','Where MovieID ='.$value);
				$this->dataDel('comment','Where AppId = 1 and IndexID ='.$value);
			}

			show_message('已成功删除',$_SERVER['HTTP_REFERER']);exit;

		}
		else
		{
			if($id)
			{
				$movie_info = $this->getDataInfo('movie','where Id = ' . $id);
				$dir = $this->sys_info['UploadPath'] .'/'. Date('m/',$movie_info['PublishDate']) . $movie_info['Id'] . '/';
				$this->html->deletedir($dir);
				$this->dataDel('movie','Where Id ='.$id);
				$this->dataDel('movie_index','Where MovieID ='.$value);
				$this->dataDel('special_index','Where MovieID ='.$value);
				$this->dataDel('comment','Where AppId = 1 and IndexID ='.$value);
				show_message('已成功删除',$_SERVER['HTTP_REFERER']);exit;
			}
			else
			{
				show_message('删除失败，请与系统开发者联系！',$_SERVER['HTTP_REFERER']);exit;
			}
		}
	}

	/**
	 * 文章分类添加
	 *
	 */
	function addCate()
	{
		if($this->in_var['request_method'] == 'post')
		{
			//这是是处理表单提交程庿
		}

		$this->template = 'article_cate_add.html';    //指定模板
	}

	/**
	 * 文章分类编辑
	 *
	 */
	function editCate()
	{
		if($this->in_var['request_method'] == 'post')
		{
			//这是是处理表单提交程庿
		}

		$this->template = 'article_cate_edit.html';    //指定模板
	}

	/**
	 * 删除分类编辑
	 *
	 */
	function delCate()
	{
		$this->listArticle();
	}

	//读取分类
	function getCategoryForSelect($root_id = 0,$currently_id = 0,$pre = '&nbsp;&#8250;&nbsp;',$level = 0)
	{

		if(!empty($this->category_list))
		{
			foreach ($this->category_list as $key => $value)
			{
				if($root_id == $value['ParentID'])
				{
					for($i=0;$i<=$level;$i++)
					{
						$currently_pre .= $pre;
					}

					if($currently_id == $value['CategoryID'])
					{
						$this->category_select_list .= '<option value="' . $value['CategoryID'] . '" selected="selected">' . $currently_pre . $value['CategoryName'] . '</option>' . "\n";
					}
					else
					{
						$this->category_select_list .= '<option value="' . $value['CategoryID'] . '">' . $currently_pre . $value['CategoryName'] . '</option>' . "\n";
					}

					unset($currently_pre);
					$this->getCategoryForSelect($value['CategoryID'],$currently_id,$pre,$level + 1);
				}
			}
		}

		return $this->category_select_list;
	}
}

/**
 * $Log$
 */
?>